package com.fauks.ssm.spring.tx_annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BookServiceImpl implements IBookService {

    @Autowired
    private IBookDao bookDao;

    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
    @Override
    public void buyBook(String username, int bookId) {
        //查询书价
        Integer bookPrice = bookDao.selectBookPriceById(bookId);
        //更新库存
        bookDao.updateBookStock(bookId);
        //更新账户
        bookDao.updateAccount(username, bookPrice);
    }
}
